// 导入db对象
const db = require('../config/db');
// 负责文章模块的数据
module.exports = {
    // 1. 纯文字：article_type: 1 ;  图文：article_type: 2 ;  图片：article_type: 3  
    addArticle(body) {
        // 处理提交的参数
        let {
            article_id,
            article_title,
            article_author,
            article_content,
            article_type,
            article_address,
            article_tags,
            article_hot,
            nav_name,
            user_id
        } = body;

        // 获取发布的时间
        let dt = new Date();
        let article_create_time = dt.toLocaleDateString() + " " + dt.toLocaleTimeString();

        // sql语句
        let sql = `insert into c_article(
            article_id,
            article_title,
            article_author,
            article_create_time,
            article_content, 
            article_type,
            article_address,
            article_tags,
            article_hot,
            nav_name,
            user_id
            ) values(
                '${article_id}',
                '${article_title}',
                '${article_author}',
                '${article_create_time}',
                '${article_content}',
                '${article_type}',
                '${article_address}',
                '${article_tags}',
                '${article_hot}',
                '${nav_name}',
                '${user_id}')`;

        //返回发布文章的promise实例
        return new Promise((resolve, reject) => {
            // 执行sql语句
            db.query(sql)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            msg: '发布文章成功。'
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '发布文章失败。',
                            err
                        })
                    }
                )
        })
    },
    // 2. 获取文章(默认第一页，10条数据)
    listArticle(page = 1, pageSize = 10) {
        let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
        let page_size = (parseInt(pageSize))
        // console.log({page_num,page_size})

        let sql = `select * from c_article limit ${page_num} , ${page_size}`;
        //返回获取文章的promise实例
        return new Promise((resolve, reject) => {
            db.query(`select count(*) as total from c_article`)
                .then(
                    arr => {
                        let total = arr[0].total;
                        return total
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计文章失败。'
                        })
                    }
                )
                .then(
                    (total) => {
                        // 执行sql语句
                        db.query(sql)
                            .then(
                                rows => {
                                    resolve({
                                        code: 200,
                                        msg: '获取文章列表成功。',
                                        result: [...rows],
                                        total,
                                        page_num: page_num + 1,
                                        page_size
                                    })
                                }
                            )
                            .catch(
                                err => {
                                    reject({
                                        code: -1,
                                        msg: '获取文章列表失败。',
                                        err
                                    })
                                }
                            )
                    }
                )

        })
    },
    // 3. 删除文章
    removeArticle(article_id) {
        // 处理删除文章的逻辑
        return new Promise((resolve, reject) => {
            db.query(`select article_id from c_article where article_id = '${article_id}'`)
                .then(
                    rows => {
                        if (rows.length == 1) {
                            // 删除数据
                            let sql = `delete from c_article where article_id = '${article_id}'`;
                            db.query(sql)
                                .then(
                                    rows => {
                                        resolve({
                                            code: 200,
                                            msg: '删除文章成功。'
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: '删除文章失败。',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '这篇文章已不存在了。'
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    },
    // 4. 根据导航获取攻略文章列表
    navArticle(nav_name, page = 1, pageSize = 30) {
        let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
        let page_size = (parseInt(pageSize))
        return new Promise((resolve, reject) => {
            db.query(`select * from c_article where nav_name = '${nav_name}' limit ${page_num} , ${page_size}`)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            msg: '获取当前导航文章成功。',
                            result: [...rows]
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    },
    // 5. 关键字搜索攻略文章
    searchArticle(keyword) {
        return new Promise((resolve, reject) => {
            const sql = `select * from c_article where article_title like '%${keyword}%' or nav_name like '%${keyword}%' or article_author like '%${keyword}%'`;
            db.query(sql)
                .then(
                    rows => {
                        // console.log('xxx,',rows)
                        resolve({
                            code: 200,
                            msg: '关键字搜索文章成功。',
                            result: [...rows]
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    },
    // 6. 根据地址获取攻略文章列表
    addressArticle(article_address, page = 1, pageSize = 30) {
        let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
        let page_size = (parseInt(pageSize))
        return new Promise((resolve, reject) => {
            db.query(`select * from c_article where article_address like '%${article_address}%' limit ${page_num} , ${page_size}`)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            msg: '获取当前地址文章成功。',
                            result: [...rows]
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    },

    // 7.  获取文章详情
    detailArticle(article_id) {
        // 获取文章详情
        return new Promise((resolve, reject) => {
            db.query(`select * from c_article where article_id = '${article_id}'`)
                .then(
                    rows => {
                        // 把从数据库查询的数据响应给前端
                        resolve({
                            code: 200,
                            msg: '查询文章成功。',
                            result: [...rows]
                        })
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    }
}